import { SvgSymbolParser } from "./svgsymbolparser";
import { LineGradientParser } from "./linearGradientparser";
class SvgDefParser{
    constructor(){
        this.symbolList = [];
        this.linegradientList = [];
        this.filterList = [];
    }
    parse(mainEle){
        let symbolParseSvc = new SvgSymbolParser();
        let childEles = mainEle.children;
        if (childEles.length > 0) {
            for (let i = 0; i < childEles.length; i++) {
                const childEle = childEles[i];
                let tagName = childEle.tagName;
                if (tagName == 'style'){
                    console.log(childEle.textContent);
                }
                else if (tagName == 'symbol'){
                    let symbol = symbolParseSvc.parse(childEle);
                    if (symbol){
                        this.symbolList.push(symbol); 
                    }
                }
                else if (tagName == 'linearGradient'){
                    let linegradientInfo = LineGradientParser.parse(childEle);
                    if (linegradientInfo){
                        this.linegradientList.push(linegradientInfo); 
                    }
                }
            }
        }
        this.processDefsRefInfo(this.linegradientList);
        console.log(this.linegradientList);
    }

    processDefsRefInfo(linegradientArr){
        let count = linegradientArr.length;
        for (let i = 0;i < count;i++){
            let info = linegradientArr[i];
            if (info.stopArr.length == 0){
                this.setLinegraddientRefInfo(info,linegradientArr);
            }
        }
    }

    setLinegraddientRefInfo(destInfo,linegradientArr){
        let xlink = destInfo.xlink;
        if (!xlink || (xlink && xlink.length == 0)){
            return ;
        }
        let refInfo = null;
        linegradientArr.forEach(ele => {
            if (ele.id == xlink){
                refInfo = ele;
                return ;
            }
        });

        if (refInfo){
            refInfo.stopArr.forEach(stop => {
                let newStop = {id:stop.id,color:stop.color,offset:stop.offset,opacity:stop.opacity};
                destInfo.stopArr.push(newStop);
            });
        }
    }
}

export {SvgDefParser}